package com.itheima.mapper;

import com.itheima.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;
/*
    采用注解的方式
    @Select(sql):查询
    @Update(sql):更新
    @Delete(sql):删除
    @Insert(sql):新增

 */

public interface RoleMapper {
    //查询所有
    @Select("select * from db")

    /*
     @Results作用等同于<resultMap>
     id: 唯一标识
     value: 封装对象属性与查询列的集合
     @Result:真正封装对象属性与查询列一一隐射关系
        id:布尔型的值,默认是false(非主键列的封装),true(主键列封装)
        column:查询列
        property:对应封装的对象属性


     */
    @Results(id="roleMap", value = {
            @Result(id=true,column = "id" ,property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "country",property = "country")
    })
    List<Role> findAll();


    @Select("select * from db where id=#{id}")
    @ResultMap("roleMap")
    Role findById(int id);


    //动态sql语句
  //  @Select("select * from db where id=#{id}")
    @Select("<script>" +
            "select * from db"+
            "<where>" +
            "<if test='value!=0'>" +
            "id=#{id}"+
            "</if>" +
            "</where>" +
            "</script>")
    List<Role> findById2(int id);



    @Select("<script>" +
            "select * from db" +
            "<where>" +
            "<if test='array!=null and array.length>0'>" +
            "<foreach collection='array' open='id in(' close=')' item='id' separator=','>" +
            "#{id}" +
            "</foreach>" +
            "</if>" +
            "</where>" +
            "</script>")
    List<Role> findById3(Integer[] ids);
}
